package cn.qin.xhangapi.controller;

import cn.qin.xhangapi.pojo.SysRole;
import cn.qin.xhangapi.pojo.SysUsers;
import cn.qin.xhangapi.service.SysRoleService;
import cn.qin.xhangapi.service.SysUsersService;
import cn.qin.xhangapi.util.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/api/role")
@RestController
@CrossOrigin
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysUsersService sysUsersService;
    //获取角色列表
    @GetMapping("/getRole")
    public ResponseResult getRoleList() {
        return ResponseResult.success(sysRoleService.list());
    }


    // 分页获取角色列表
    @GetMapping("/getRoleList")
    public ResponseResult getRoleList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<SysRole> pageInfo = new Page<>(pageNo,pageSize);
        sysRoleService.page(pageInfo);

        return ResponseResult.success(pageInfo);
    }

    //根据id获取角色信息
    @GetMapping("/getRoleById")
    public ResponseResult getRole(@RequestParam("id") Long id){
        return ResponseResult.success(sysRoleService.getById(id));
    }

    //修改角色信息
    @PutMapping("/updateRole")
    public ResponseResult updateRole(
            @RequestParam("id") Long id,
            @RequestParam("roleName") String roleName,
            @RequestParam("remarks") String remarks
    ){
        // 根据角色组id判断该角色组是否存在
        if(sysRoleService.getById(id) == null) {
            return ResponseResult.failure("该系统用户角色组不存在");
        }

        SysRole role = new SysRole();
        role.setId(id);
        role.setRoleName(roleName);
        role.setRemarks(remarks);
        if (sysRoleService.updateById(role)) {
            return ResponseResult.success("修改成功");
        }
        return ResponseResult.failure("修改失败");
    }

    //新增角色信息
    @PostMapping("/addRole")
    public ResponseResult addRole(
            @RequestParam("roleName") String roleName,
            @RequestParam("remarks") String remarks
    ){
        SysRole role = new SysRole();
        role.setRoleName(roleName);
        role.setRemarks(remarks);
        if (sysRoleService.save(role)) {
            return ResponseResult.success("添加成功");
        }
        return ResponseResult.failure("添加失败");
    }

    //删除角色信息
    @DeleteMapping("/deleteRole")
    public ResponseResult deleteRole(@RequestParam("id") Long id){
        SysRole sysRole = sysRoleService.getById(id);

        // 根据角色组id判断该角色组是否存在
        if(sysRoleService.getById(id) == null) {
            return ResponseResult.failure("该系统用户角色组不存在");
        }

        // 根据is_super判断该角色组是否能删除
        // is_super == 1，即最高系统用户角色组，禁止删除
        if(sysRole.getIsSuper() == 1) {
            return ResponseResult.success("该系统用户角色组禁止删除");
        }

        // 判断该角色组下是否有用户
        if(sysUsersService.count(new QueryWrapper<SysUsers>().eq("role_id", id)) > 0) {
            return ResponseResult.failure("该系统用户角色组下还有系统用户账户，无法删除");
        }

        if(sysRoleService.removeById(id)){
            return ResponseResult.success("删除成功");
        }
        return ResponseResult.failure("删除失败");
    }
}
